<?php

namespace app\workers\controller;

use app\admin\service\EchartsService;
use app\model\WorkersTaskUserResultContinuityModel;
use app\model\WorkersTaskUserResultModel;
use think\App;

class Echarts extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 生成word团体报告
     * @return bool|string
     */
    public function GroupReport(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $EchartsService = new EchartsService($this->app);
            return success($EchartsService->GroupReport($data));
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word任务报告
     * @return bool|string
     */
    public function TaskWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $TaskUserResultModel = new WorkersTaskUserResultModel();
            $info = $TaskUserResultModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $res = $EchartsService->SelfWord($data);
            $info->word = $res['word'];
            $info->pdf = $res['pdf'];
            $info->save();
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 生成word任务连评报告
     * @return bool|string
     */
    public function ContinuityWord(): bool|string
    {
        try {
            $data = $this->request->param('data');
            $data = json_decode($data, true);
            if (empty($data['unit_id'])){
                $data['unit_id'] = $this->userData['unit_id'];
            }
            $TaskUserResultContinuityModel = new WorkersTaskUserResultContinuityModel();
            $info = $TaskUserResultContinuityModel->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            $res = $EchartsService->SelfWord($data);
            $info->word = $res['word'];
            $info->pdf = $res['pdf'];
            $info->save();
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}